{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Base 7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: convertToBase7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #七进制数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个整数 <code>num</code>，将其转化为 <strong>7 进制</strong>，并以字符串形式输出。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> num = 100\n",
    "<strong>输出:</strong> \"202\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> num = -7\n",
    "<strong>输出:</strong> \"-10\"\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>-10<sup>7</sup>&nbsp;&lt;= num &lt;= 10<sup>7</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [base-7](https://leetcode.cn/problems/base-7/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [base-7](https://leetcode.cn/problems/base-7/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['100', '-7']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        flag = True\n",
    "        if num < 0:\n",
    "            flag = False\n",
    "        num = abs(num)\n",
    "        res = []\n",
    "        while num:\n",
    "            res.append(num % 7)\n",
    "            num = num // 7\n",
    "        res.reverse()\n",
    "        res = [str(i) for i in res]\n",
    "        if flag:\n",
    "            return ''.join(res)\n",
    "        else:\n",
    "            return '-'+''.join(res)\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        s = ''\n",
    "        if num > 0:\n",
    "            while num:\n",
    "                s = str(num % 7) + s\n",
    "                num //= 7\n",
    "        else:\n",
    "            num = ~num + 1\n",
    "            while num:\n",
    "                s = str(num % 7) + s\n",
    "                num //= 7\n",
    "            s = '-' + s\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        res = \"\"\n",
    "        flag = 1\n",
    "        if num < 0:\n",
    "            flag = 0\n",
    "            num = - num\n",
    "        while num:\n",
    "            res = str(num%7) + res\n",
    "            num = num // 7\n",
    "        if flag == 1:\n",
    "            return res\n",
    "        else:\n",
    "            return \"-\" + res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0: return '0'\n",
    "        d = ''\n",
    "        if num < 0:\n",
    "            num = -num\n",
    "            d = '-'\n",
    "        cur = num\n",
    "        res = ''\n",
    "        while cur:\n",
    "            c = str(cur % 7)\n",
    "            res = c + res\n",
    "            cur = cur // 7\n",
    "        res = d + res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return str(num)\n",
    "        sign = '-' if num < 0 else ''\n",
    "        answer = []\n",
    "        num = abs(num)\n",
    "        while num:\n",
    "            answer.insert(0, str(num % 7))\n",
    "            num //= 7\n",
    "        return sign + \"\".join(answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if not num: return \"0\"\n",
    "        sign, resList = \"\" if num >= 0 else \"-\", []\n",
    "        num = abs(num)\n",
    "        while num:\n",
    "            resList.append(num % 7)\n",
    "            num //= 7\n",
    "        return sign + \"\".join(map(str,resList[::-1]))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef convertToBase7(self, num: int) -> str:\n",
    "\t\treturn (str(num) if num < 7 else self.convertToBase7(num // 7) + str(\n",
    "\t\t\tnum % 7)) if num >= 0 else '-' + self.convertToBase7(-num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        n, res = abs(num), ''\n",
    "        while n:\n",
    "            res = str(n % 7) + res\n",
    "            # print(res)\n",
    "            n //= 7\n",
    "            # print(n)\n",
    "        return '-' * (num < 0) + res or \"0\"\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        s=[]\n",
    "        t=0\n",
    "        if num==0:\n",
    "            return \"0\"\n",
    "        if num<0:\n",
    "            num*=-1\n",
    "            t=1\n",
    "        while num>0:\n",
    "            k=num%7\n",
    "            s+=str(k)\n",
    "            num=num//7\n",
    "        if t==1:\n",
    "            s='-'+''.join(s[::-1])\n",
    "        else:\n",
    "            s=''.join(s[::-1])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        ans = ''\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        elif num > 0:\n",
    "            f = True\n",
    "        else:\n",
    "            num = -num\n",
    "            f = False\n",
    "        while num:\n",
    "            ans += str(num % 7)\n",
    "            num //= 7\n",
    "        return ans[::-1] if f else '-' + ans[::-1]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'  # 如果 num 为 0，直接返回字符串 '0'\n",
    "\n",
    "        result = ''  # 初始化结果字符串\n",
    "        is_negative = num < 0  # 判断是否为负数\n",
    "        num = abs(num)  # 取绝对值以便进行转换\n",
    "\n",
    "        while num > 0:\n",
    "            digit = num % 7  # 取余数作为当前位的值\n",
    "            result = str(digit) + result  # 将当前位的值添加到结果字符串的最前面\n",
    "            num //= 7  # 更新 num 为商，用于下一次循环\n",
    "\n",
    "        if is_negative:\n",
    "            result = '-' + result  # 如果是负数，在结果字符串最前面添加负号\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num < 0:\n",
    "            return \"-\" + self.convertToBase7(-num)\n",
    "        if num < 7:\n",
    "            return str(num)\n",
    "        return self.convertToBase7(num//7) + str(num%7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        s = []\n",
    "        if num==0:\n",
    "            return '0'\n",
    "        flag = bool(num>=0)\n",
    "        num = num if flag else -num\n",
    "        while num:\n",
    "            s.insert(0,(str(num%7)))\n",
    "            num//=7\n",
    "        return ('' if flag else '-') + ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "\n",
    "        x, ret = abs(num), ''\n",
    "        while x > 0:\n",
    "            ret = str(x % 7) + ret\n",
    "            x //= 7\n",
    "\n",
    "        return ret if num >= 0 else '-' + ret\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return str(0)\n",
    "        sign = num < 0\n",
    "        num = abs(num)\n",
    "        res = []\n",
    "        while num > 0:\n",
    "            res.append(str(num%7))\n",
    "            num//=7\n",
    "        return ('-' if sign else '') + ''.join(res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        a=''\n",
    "        if num<0:\n",
    "            b=1\n",
    "        else:\n",
    "            b=0\n",
    "        num=abs(num)\n",
    "        while num//7!=0:\n",
    "            a+=str(num%7)\n",
    "            num=num//7\n",
    "        a+=str(num)\n",
    "        return a[::-1] if b==0 else '-'+a[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        ans = ''\n",
    "        if num == 0: return \"0\"\n",
    "        elif num > 0: flag = 1\n",
    "        else:\n",
    "            flag = 0\n",
    "            num = abs(num)\n",
    "        while num >= 7:\n",
    "            ans += str(num % 7)\n",
    "            num //= 7\n",
    "        if num != 0:\n",
    "            ans += str(num)\n",
    "        if flag == 1:\n",
    "            return ans[::-1]\n",
    "        else:\n",
    "            ans += '-'\n",
    "            return ans[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        negative = False\n",
    "        if num < 0:\n",
    "            negative =True\n",
    "            num = -num\n",
    "        lst = []\n",
    "        ans = ''\n",
    "        while num > 0:\n",
    "            remainder = num % 7\n",
    "            num //= 7\n",
    "            lst.append(remainder)\n",
    "        for i in range(len(lst)-1,-1,-1):\n",
    "            ans = ans + str(lst[i])\n",
    "\n",
    "        \n",
    "        return ans if not negative else '-' + ans\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0:\n",
    "            return \"0\"\n",
    "        x = abs(num)\n",
    "        i = 0\n",
    "        while x>=7**i:\n",
    "            i+=1\n",
    "        l = \"\"\n",
    "        for k in range(i):\n",
    "            k1=1\n",
    "            while(7**(i-k-1)*k1<=x):\n",
    "                k1+=1\n",
    "            l+=str(k1-1)\n",
    "            x-=7**(i-k-1)*(k1-1)\n",
    "        if(num)<=0:\n",
    "            return \"-\"+l\n",
    "        else:\n",
    "            return l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        s=\"\"\n",
    "        n=abs(num)\n",
    "        while n>0:\n",
    "            s=str(n%7)+s\n",
    "            n//=7\n",
    "        if num>0:\n",
    "            return s\n",
    "        elif num==0:\n",
    "            return \"0\"\n",
    "        else:\n",
    "            return \"-\"+s        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        temp_num = abs(num)\n",
    "        result = \"\"\n",
    "        while temp_num > 0:\n",
    "            result = str(temp_num % 7) + result\n",
    "            temp_num = temp_num // 7\n",
    "        if num < 0:\n",
    "            result = \"-\" + result\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num ==0:\n",
    "            return \"0\"\n",
    "        f = (num<0)\n",
    "        if num < 0:\n",
    "            num = -num\n",
    "    \n",
    "        digits = []\n",
    "        while num:\n",
    "            digits.append(str(num%7))\n",
    "            num//=7\n",
    "        if f:\n",
    "            digits.append('-')\n",
    "        return ''.join(reversed(digits))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        negative = -1 if num < 0 else 1\n",
    "        num = abs(num)\n",
    "        res = []\n",
    "        while num:\n",
    "            res.append(str(num%7))\n",
    "            num //= 7\n",
    "        if negative <0 :\n",
    "            res.append('-')\n",
    "        return \"\".join(reversed(res))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        flag = True\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        if num < 0:\n",
    "            flag = False\n",
    "            num = abs(num)\n",
    "        result = \"\"\n",
    "        while num > 0:\n",
    "            result += str(num % 7)\n",
    "            num //= 7\n",
    "        if flag == True:\n",
    "            return result[::-1]\n",
    "        else:\n",
    "            return \"-\" + result[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        flag= False\n",
    "        if num < 0:\n",
    "            num =-num\n",
    "            flag = True\n",
    "        ans = []\n",
    "        while num:\n",
    "            ans.append(str(num % 7))\n",
    "            num //= 7\n",
    "        return ''.join(ans[::-1]) if not flag else '-' + ''.join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        final = \"\"\n",
    "        neg = False\n",
    "        if num < 0:\n",
    "            neg, num = True, -num\n",
    "        while num:\n",
    "            num, rem = divmod(num, 7)\n",
    "            # print(num, rem)\n",
    "            final = str(rem) + final\n",
    "        return (\"-\" if neg else \"\") + final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0:\n",
    "            return '0'\n",
    "        alphabet='0123456'\n",
    "        temp=[]\n",
    "        sign='-'if num<0 else ''\n",
    "        num=abs(num)\n",
    "        while num:\n",
    "            temp.append(num%7)\n",
    "            num//=7\n",
    "        return sign+''.join(alphabet[i]for i in temp[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def convertToBase7(self, num: int) -> str:\r\n",
    "        if num == 0: return '0'\r\n",
    "        is_negative = False\r\n",
    "        res = []\r\n",
    "        if num < 0:\r\n",
    "            is_negative = True\r\n",
    "            num *= -1\r\n",
    "        while num:\r\n",
    "            res.append(str(num % 7))\r\n",
    "            num //= 7\r\n",
    "        return '-' * is_negative + ''.join(res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def convertToBase7(self, num: int) -> str:\n",
    "#         if num==0:\n",
    "#             return \"0\"\n",
    "#         neg = num<0\n",
    "#         digits=[]\n",
    "#         num=abs(num)\n",
    "#         while num:\n",
    "#             digits.append(str(num%7))\n",
    "#             num//=7\n",
    "#         if neg:\n",
    "#             digits.append(\"-\")\n",
    "#         return \"\".join(reversed(digits))\n",
    "#         # return \"\".join(digits[::-1])\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0:\n",
    "            return \"0\"\n",
    "        neg = num<0\n",
    "        digits=\"\"\n",
    "        num=abs(num)\n",
    "        while num:\n",
    "            digits+=str(num%7)\n",
    "            num//=7\n",
    "        if neg:\n",
    "            digits+=\"-\"\n",
    "        return \"\".join(reversed(digits))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num >= 0:\n",
    "            a = 1\n",
    "        else:\n",
    "            a = -1\n",
    "        def toStr(n,base):\n",
    "            convertString = '0123456789ABCDEF'\n",
    "            if n < base:\n",
    "                return convertString[n]\n",
    "            else:\n",
    "                return toStr(n//base,base) + convertString[n%base]\n",
    "        num = abs(num)\n",
    "        return str(a*int(toStr(num,7)))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        \n",
    "        ans = []\n",
    "        flag = False\n",
    "        if num<0:\n",
    "            flag = True\n",
    "            num = abs(num)\n",
    "        while num>0:\n",
    "            ans.append(str(num%7))\n",
    "            num = num//7\n",
    "        if flag:\n",
    "            ans.append(\"-\")\n",
    "        return \"\".join(reversed(ans))\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        base = 7\n",
    "        if not num: return str(num)\n",
    "        #记录正负情况\n",
    "        flag = False\n",
    "        if num < 0: flag = True\n",
    "        num = abs(num)\n",
    "        res = []\n",
    "        while num:\n",
    "            res.append(str(num % base))#注意要存str而不是int\n",
    "            num = num // base\n",
    "        #注意res要取反方向\n",
    "        return (\"-\" if flag else \"\") + (''.join(res[::-1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        if num < 0:\n",
    "            new_num = -num\n",
    "        else:\n",
    "            new_num = num\n",
    "        res = []\n",
    "        while new_num != 0:\n",
    "            left = new_num % 7\n",
    "            new_num //= 7\n",
    "            res.append(left)\n",
    "        tar = ''.join([str(i) for i in res[::-1]])\n",
    "        if num < 0:\n",
    "            return '-'+tar\n",
    "        return tar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        \n",
    "        x, ret = abs(num), ''\n",
    "        # x, y = x // 7, x % 7\n",
    "        while x > 0:\n",
    "            x, y = x // 7, x % 7\n",
    "            ret = str(y) + ret\n",
    "\n",
    "        return ret if num >= 0 else '-' + ret\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if not num:\n",
    "            return '0'\n",
    "        sign = '' if num >= 0 else '-'\n",
    "        num = num if num >= 0 else -num\n",
    "        digit = \"\"\n",
    "        while num > 0:\n",
    "            digit += str(num % 7)\n",
    "            num = num // 7\n",
    "        return sign + digit[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        num_a=abs(num)\n",
    "        ans=[]\n",
    "        if(num==0):\n",
    "            return \"0\"\n",
    "        while num_a>0:\n",
    "            a=num_a%7\n",
    "            num_a=num_a//7\n",
    "            ans.append(str(a))\n",
    "        if(num<0):\n",
    "            return \"-\"+\"\".join(ans[::-1])\n",
    "        else:\n",
    "            return \"\".join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "      neg = num<0\n",
    "      num= abs(num)\n",
    "      ret = \"\"\n",
    "      while num>0:\n",
    "        num,rem = divmod(num,7)\n",
    "        ret = str(rem) + ret\n",
    "      if not ret:\n",
    "        ret = \"0\"\n",
    "      if neg:\n",
    "        ret = \"-\"+ret\n",
    "      return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        num_a = abs(num)\n",
    "        if(num==0):\n",
    "            return \"0\"\n",
    "        ans=[]\n",
    "        while num_a>0:\n",
    "            a=num_a%7\n",
    "            num_a=num_a//7\n",
    "            ans.append(str(a))\n",
    "        if(num<0):\n",
    "            return \"-\"+\"\".join(ans[::-1])\n",
    "        else:\n",
    "            return \"\".join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        n = abs(num)\n",
    "        if num ==0 :\n",
    "            return \"0\"\n",
    "        res = []\n",
    "        while n != 0:\n",
    "            y = n % 7\n",
    "            n = n // 7\n",
    "            res.append(str(y))\n",
    "        res.reverse()\n",
    "        res = ''.join(res)\n",
    "        if num<0:\n",
    "            return '-'+res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        ans = ''\n",
    "\n",
    "        abs_num = abs(num)\n",
    "\n",
    "        index = 0\n",
    "\n",
    "        while abs_num >= 7 **(index):\n",
    "            ans = str(int(abs_num%(7**(index+1))/(7**index)))+ ans\n",
    "            abs_num -= int(abs_num%(7**(index+1))/(7**index))\n",
    "            index += 1\n",
    "        \n",
    "        if num < 0:\n",
    "            return '-'+ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0: return '0'\n",
    "        digi = ''\n",
    "        if num<0:\n",
    "            num = abs(num)\n",
    "            digi = '-'\n",
    "        res = ''\n",
    "        num_yushu = num\n",
    "        while num != 0 :\n",
    "            num_yushu = num % 7\n",
    "            num = num // 7\n",
    "            res = str(num_yushu)+res\n",
    "            # res = ''.join([str(num_yushu),res])\n",
    "        res = ''.join([digi,res])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        s=''\n",
    "        if num>=0:\n",
    "            while num>6:\n",
    "                s=str(num%7)+s\n",
    "                num=num//7\n",
    "            s=str(num)+s\n",
    "        else:\n",
    "            num=-num\n",
    "            while num>6:\n",
    "                s=str(num%7)+s\n",
    "                num=num//7\n",
    "            s=str(num)+s\n",
    "            s='-'+s\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        ans = ''\n",
    "        flag = False\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        if num < 0:\n",
    "            num = -num\n",
    "            flag = True\n",
    "        while num:\n",
    "            num, m = divmod(num, 7)\n",
    "            ans = str(m) + ans\n",
    "        return ans if not flag else '-' + ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        ans = \"\"\n",
    "        negative = False\n",
    "        if num<0:\n",
    "            num*=-1\n",
    "            negative = True\n",
    "\n",
    "        while num>=7:\n",
    "            num,residual =num//7, num%7\n",
    "            ans = str(residual)+ans\n",
    "        if num!=0 or ans==\"\":\n",
    "            ans =str(num)+ans\n",
    "        if negative:\n",
    "            return \"-\"+ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "\n",
    "        res = \"\"\n",
    "        if num < 0:\n",
    "            num = -num\n",
    "            flag = False\n",
    "        else:\n",
    "            flag = True\n",
    "        while num != 0:\n",
    "            res = str(num % 7) + res\n",
    "\n",
    "            num = num//7\n",
    "            # print(num)\n",
    "        if flag:\n",
    "            return res\n",
    "        else:\n",
    "            return '-'+res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        res = ''\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        elif num < 0:\n",
    "            pre = '-'\n",
    "            num = -num\n",
    "        else:\n",
    "            pre=''\n",
    "        while num > 0:\n",
    "            res = f\"{num%7}\" + res\n",
    "            num //= 7\n",
    "        return pre+res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        ans = ''\n",
    "\n",
    "        abs_num = abs(num)\n",
    "\n",
    "        index = 0\n",
    "\n",
    "        while abs_num >= 7 **(index):\n",
    "            ans = str(int(abs_num%(7**(index+1))/(7**index)))+ ans\n",
    "            abs_num -= int(abs_num%(7**(index+1))/(7**index))\n",
    "            index += 1\n",
    "        \n",
    "        if num < 0:\n",
    "            return '-'+ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        res = ''\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        elif num < 0:\n",
    "            pre = '-'\n",
    "            num = -num\n",
    "        else:\n",
    "            pre=''\n",
    "        while num > 0:\n",
    "            res = f\"{num%7}\" + res\n",
    "            num //= 7\n",
    "        return pre+res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        res = \"\"\n",
    "        ans = -num if num < 0 else num\n",
    "        if num == 0:    return \"0\"\n",
    "        while ans != 0:\n",
    "            div = ans // 7\n",
    "            remain = ans - 7 * div\n",
    "            res += str(remain)\n",
    "            ans = div\n",
    "        return res[::-1] if num >= 0 else (\"-\" + res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0: return '0'\n",
    "        digi = ''\n",
    "        if num<0:\n",
    "            num = abs(num)\n",
    "            digi = '-'\n",
    "        res = ''\n",
    "        num_yushu = num\n",
    "        while num != 0 :\n",
    "            num_yushu = num % 7\n",
    "            num = num // 7\n",
    "            res = ''.join([str(num_yushu),res])\n",
    "        res = ''.join([digi,res])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        b=abs(num)\n",
    "        y=''\n",
    "        while b>=7:\n",
    "            a=b%7\n",
    "            b=b//7\n",
    "            y=str(a)+y\n",
    "        if num>=0:\n",
    "            return str(b)+y\n",
    "        else:\n",
    "            return '-'+str(b)+y\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0:\n",
    "            return '0'\n",
    "        if num<0:\n",
    "            return '-'+self.convertToBase7(-num)\n",
    "        ans = ''\n",
    "        while num:\n",
    "            ans = str(num%7)+ans\n",
    "            num//=7\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0:return '0'\n",
    "        t=''\n",
    "        if num<0:\n",
    "            t='-'\n",
    "            num=-num\n",
    "        s=''\n",
    "        while num>0:\n",
    "            x=num//7\n",
    "            y=num%7\n",
    "            s=str(y)+s\n",
    "            num=x\n",
    "\n",
    "        return t+s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        result=''\n",
    "        if num==0:\n",
    "            return \"0\"\n",
    "        if num<0:\n",
    "            flag='-'\n",
    "        else:\n",
    "            flag=''\n",
    "        num=abs(num)\n",
    "        while num>0:\n",
    "            result=str(num%7)+result\n",
    "            num=num//7\n",
    "        return flag+result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        if num<0:\n",
    "            sign = '-'\n",
    "        else:\n",
    "            sign = ''\n",
    "        num = abs(num)\n",
    "        digits = []\n",
    "        while num:            \n",
    "            digits.append(str(num%7))\n",
    "            num = num//7\n",
    "\n",
    "        s = sign + ''.join(digits[::-1])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num < 0: \n",
    "            return '-' + self.convertToBase7(-num)\n",
    "        if num < 7: \n",
    "            return str(num)\n",
    "        return self.convertToBase7(num // 7) + str(num % 7)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if not num: return str(0)\n",
    "        s = ''\n",
    "        absn = abs(num)\n",
    "        while absn:\n",
    "            absn, n = divmod(absn, 7)\n",
    "            s = str(n) + s\n",
    "        return s if num >= 0 else '-' + s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        data = []\n",
    "        sign = \"-\" if num < 0 else \"\"\n",
    "        num = abs(num)\n",
    "        while num > 0:\n",
    "            data.append(str(num % 7))\n",
    "            num //= 7\n",
    "        strs = \"\".join(var for var in data[::-1])\n",
    "        return sign + strs\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "            \n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0: return '0'\n",
    "        if num < 0: return '-' + self.convertToBase7(-num);\n",
    "        ans = ''\n",
    "        while num > 0:\n",
    "            mod = num % 7\n",
    "            ans = str(mod) + ans\n",
    "            num //= 7\n",
    "            \n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 100\n",
    "    # 100   //7：14   %7：2\n",
    "    # 14    //7：2    %7：0\n",
    "    # 2     //7：2    %7：2\n",
    "    # 202(7) 2+0*7^1+2*7^2=100\n",
    "\n",
    "    # 12345\n",
    "    # 地板除和模是相配的：num=num//k+num%k\n",
    "    # 模用来去前缀，除法去后缀。或者说除法保留大得几位，模保留小的几位。\n",
    "    # //10 1234\n",
    "    # //100 123\n",
    "\n",
    "    # 保证num不是负数\n",
    "    def f(self,num):\n",
    "        if num<7:return str(num)\n",
    "        return self.convertToBase7(num//7)+str(num%7)\n",
    "\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        return self.f(num) if num>=0 else \"-\"+self.f(-1*num)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 100\n",
    "    # 100   //7：14   %7：2\n",
    "    # 14    //7：2    %7：0\n",
    "    # 2     //7：2    %7：2\n",
    "    # 202(7) 2+0*7^1+2*7^2=100\n",
    "\n",
    "    # 12345\n",
    "    # 地板除和模是相配的：num=num//k+num%k\n",
    "    # 模用来去前缀，除法去后缀。或者说除法保留大得几位，模保留小的几位。\n",
    "    # //10 1234\n",
    "    # //100 123\n",
    "\n",
    "    # 需保证num不是负数\n",
    "    def f(self,num):\n",
    "        # 小于7不用继续往前推了，返回\n",
    "        if num<7:return str(num)\n",
    "\n",
    "        # 理解下面这点特别重要：\n",
    "        # self.convertToBase7(num//7)：前面几位\n",
    "        # str(num%7)当前的位\n",
    "        return self.f(num//7)+str(num%7)\n",
    "\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        return self.f(num) if num>=0 else \"-\"+self.f(-1*num)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0:\n",
    "            return '0'\n",
    "        is_neg=False\n",
    "        if num<0:\n",
    "            is_neg=True\n",
    "            num=-num\n",
    "        max_pos=int(log(num,7))\n",
    "        result=''\n",
    "        for i in range(max_pos,-1,-1):\n",
    "            result+=str(num//pow(7,i))\n",
    "            num=num%pow(7,i)\n",
    "        if is_neg:\n",
    "            return '-'+result\n",
    "        else:\n",
    "            return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        ans=''\n",
    "        f=''\n",
    "        if num==0:\n",
    "            return '0'\n",
    "        if num<0:\n",
    "            f='-'\n",
    "            num=-num\n",
    "        while num>0:\n",
    "            ans=str(num%7)+ans\n",
    "            num//=7\n",
    "        return f+ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0:\n",
    "            return \"0\"\n",
    "        neg = num<0\n",
    "        digits=[]\n",
    "        num=abs(num)\n",
    "        while num:\n",
    "            digits.append(str(num%7))\n",
    "            num//=7\n",
    "        if neg:\n",
    "            digits.append(\"-\")\n",
    "        return \"\".join(digits[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 100\n",
    "    # 100   //7：14   %7：2\n",
    "    # 14    //7：2    %7：0\n",
    "    # 2     //7：2    %7：2\n",
    "    # 202(7) 2+0*7^1+2*7^2=100\n",
    "\n",
    "    # 12345\n",
    "    # 地板除和模是相配的：num=num//k+num%k\n",
    "    # 模用来去前缀，除法去后缀。或者说除法保留大得几位，模保留小的几位。\n",
    "    # //10 1234\n",
    "    # //100 123\n",
    "\n",
    "    # 需保证num不是负数\n",
    "    def f(self,num):\n",
    "        # 小于7不用继续往前推了，返回\n",
    "        if num<7:return str(num)\n",
    "\n",
    "        # 理解下面这点特别重要：\n",
    "        # self.convertToBase7(num//7)：前面几位\n",
    "        # str(num%7)当前的位\n",
    "        return self.f(num//7)+str(num%7)\n",
    "\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        return self.f(num) if num>=0 else \"-\"+self.f(-1*num)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num==0: return \"0\"\n",
    "        res = \"\"\n",
    "        posi = True if num>0 else False\n",
    "        if not posi:\n",
    "            num = -num\n",
    "        while num!=0:\n",
    "            d = num % 7\n",
    "            num = num // 7\n",
    "            res = str(d) + res\n",
    "        if not posi:\n",
    "            res = '-' + res\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        result=''\n",
    "        if num==0:\n",
    "            return \"0\"\n",
    "        if num<0:\n",
    "            flag='-'\n",
    "        else:\n",
    "            flag=''\n",
    "        num=abs(num)\n",
    "        while num>0:\n",
    "            result=result+str(num%7)\n",
    "            num=num//7\n",
    "        return flag+result[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "\n",
    "        ans = []\n",
    "        res = \"\"\n",
    "        flag = 1\n",
    "        if num < 0 :\n",
    "            flag = -1\n",
    "        num = abs(num)\n",
    "        while num>0:\n",
    "            ans.append(str(num%7))\n",
    "            num = num//7\n",
    "        ans = reversed(ans)\n",
    "        res = ''.join(ans)\n",
    "        return str(res) if flag==1 else \"-\"+str(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 100\n",
    "    # 100   //7：14   %7：2\n",
    "    # 14    //7：2    %7：0\n",
    "    # 2     //7：2    %7：2\n",
    "    # 202(7) 2+0*7^1+2*7^2=100\n",
    "\n",
    "    # 12345\n",
    "    # 地板除和模是相配的：num=num//k+num%k\n",
    "    # 模用来去前缀，除法去后缀。或者说除法保留大得几位，模保留小的几位。\n",
    "    # //10 1234\n",
    "    # //100 123\n",
    "\n",
    "    # 需保证num不是负数\n",
    "    def f(self,num):\n",
    "        # 小于7不用继续往前推了，返回\n",
    "        if num<7:return str(num)\n",
    "\n",
    "        # 理解下面这点特别重要：\n",
    "        # self.convertToBase7(num//7)：前面几位\n",
    "        # str(num%7)当前的位\n",
    "        return self.f(num//7)+str(num%7)\n",
    "\n",
    "    def convertToBase7(self, num: int) -> str:\n",
    "        return self.f(num) if num>=0 else \"-\"+self.f(-1*num)\n",
    "        \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
